package edu.dublbo.app.generator.util;

import edu.dublbo.app.generator.entity.TBasicModelDetail;

import java.util.ArrayList;
import java.util.List;

/**
 * @author DubLBo
 * @since 2020-09-07 17:16
 * i believe i can i do
 */
public class DemoUtils {

    public static final List<TBasicModelDetail> DEFAULT_PROS = new ArrayList<>();

    public static final String DEFAULT_TABLE_PREFIX = "t_";

    static {
        DEFAULT_PROS.add(new TBasicModelDetail("id", 1, "id", 1, 32, "主键ID", 0, Constant.CURRENT_USER, Constant.CURRENT_USER, 1, 1));
        DEFAULT_PROS.add(new TBasicModelDetail("createUser", 1, "create_user", 1, null, "创建人", 0, Constant.CURRENT_USER, Constant.CURRENT_USER, 0, 996));
        DEFAULT_PROS.add(new TBasicModelDetail("createTime", 3, "create_time", 10, null, "创建时间", 0, Constant.CURRENT_USER, Constant.CURRENT_USER,0, 997));
        DEFAULT_PROS.add(new TBasicModelDetail("modifyUser", 1, "modify_user", 1, null, "最近修改人", 0, Constant.CURRENT_USER, Constant.CURRENT_USER,0, 998));
        DEFAULT_PROS.add(new TBasicModelDetail("modifyTime", 3, "modify_time", 10, null, "最近修改时间", 0, Constant.CURRENT_USER, Constant.CURRENT_USER,0, 999));
        DEFAULT_PROS.add(new TBasicModelDetail("deleteFlag", 2, "delete_flag", 5, null, "是否删除（1：是，0：否）", 0, Constant.CURRENT_USER, Constant.CURRENT_USER, 0, 1000));
    }
    public static String modelName2TableName(String name) {
        return modelName2TableName(name, DEFAULT_TABLE_PREFIX);
    }

    /**
     * 将模型名转化为表名
     *
     * @param modelName 模型名（不带包路径），建议包含前缀
     * @return 表名
     */
    public static String modelName2TableName(String modelName, String tablePrefix) {
        if (modelName == null || modelName.length() == 0) {
            return "";
        }

        StringBuilder sb = new StringBuilder();
        sb.append(tablePrefix).append(modelName.substring(0,1).toLowerCase());
        for (int i = 1; i < modelName.length(); i++) {
            char target = modelName.charAt(i);
            if (target >= 'A' && target <= 'Z') {
                sb.append("_").append((char) (target + 32));
            } else {
                sb.append(target);
            }
        }
        return sb.toString();
    }

    public static String genBusiBeanName(String modelName) {
        if (modelName == null || modelName.length() < 3) return "";
        char first = modelName.charAt(0);
        char second = modelName.charAt(1);
        // 去除默认前缀
        if ((first == 'T' || first == 'V' || first == 'S') && Character.isUpperCase(second)) {
            return modelName.substring(1, 2).toUpperCase() + modelName.substring(2);
        }
        return modelName.substring(0, 1).toUpperCase() + modelName.substring(1);
    }

    /**
     * 生成模型明细中的下一个序号
     * @param array 已有的序号
     */
    public static Integer generateNextSortNo(List<Integer> array){
        if(array == null || array.size() == 0){
            return Constant.MODEL_DETAIL_SORTNO_MIN_POS;
        }
        int var1 = Integer.MIN_VALUE;
        int var2 = Integer.MAX_VALUE;
        for (Integer tmp : array) {
            if (tmp != null) {
                // 寻找比最小基准数 小的 最大的数
                if (tmp < Constant.MODEL_DETAIL_SORTNO_MIN_POS && tmp > var1) {
                    var1 = tmp;
                }
                // 寻找比最大基数 大的 最小的数
                if (tmp > Constant.MODEL_DETAIL_SORTNO_MAX_POS && tmp < var2) {
                    var2 = tmp;
                }
            }
        }

        // 在最小的基数范围下的数已经取完
        if (var1 + 1 == Constant.MODEL_DETAIL_SORTNO_MIN_POS) {
            return var2 == Integer.MAX_VALUE ? Constant.MODEL_DETAIL_SORTNO_MAX_POS + 1 : var2 + 1;
        }
        return var1 == Integer.MIN_VALUE ? Constant.MODEL_DETAIL_SORTNO_MIN_POS - 1 : var1 + 1;
    }

    public static String field2SetMethodName(String fieldName) {
        if (fieldName == null || fieldName.trim().length() == 0) {
            return "";
        }
        return "set" + fieldName.substring(0, 1).toUpperCase() +
                fieldName.substring(1);
    }

    public static String field2GetMethodName(String fieldName) {
        if (fieldName == null || fieldName.trim().length() == 0) {
            return "";
        }
        return "get" + fieldName.substring(0, 1).toUpperCase() +
                fieldName.substring(1);
    }


}
